/*
 * Copyright (c) 2008 by 1eEurope (Switzerland) AG, Thalwil
 *
 * Customer:	1eEurope (Switzerland) AG
 * Project:		BKS
 * Created:		30.09.2008, WBL
 *
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Microsoft.SharePoint;
using System.Web.Configuration;
using System.Collections.Specialized;
using System.IO;
using System.Diagnostics;
using System.Collections;
using System.Xml;
using By1eEurope.BKS.Tools.Utils;
using System.Configuration;

namespace By1eEurope.BKS.Controls
{
    /// <summary>
    /// 
    /// </summary>
    public class MOSSCleanupModule : IHttpModule
    {
        public void Init(HttpApplication app)
        {
            app.ReleaseRequestState += new EventHandler(InstallResponseFilter);    
        }

        /// <summary>
        /// Use this method to wire a page filter.
        /// </summary>
        private void InstallResponseFilter(object sender, EventArgs e)
        {
            HttpResponse response = HttpContext.Current.Response;
            HttpRequest request = HttpContext.Current.Request;
            
            // We want to exclude some tags when we're indexing the site
            // (The crawler can be identified by the HTTP_USER_AGENT: "Mozilla/4.0 (compatible; MSIE 4.01; Windows NT; MS Search 5.0 Robot)"
            if (request.ServerVariables["HTTP_USER_AGENT"] != null && !request.ServerVariables["HTTP_USER_AGENT"].Contains("MS Search 5.0 Robot"))
            {
                if (response.ContentType == "text/html" && request.Url.AbsolutePath.EndsWith(".aspx") && 
                    !request.Url.AbsolutePath.Contains("_layouts/") && !request.Url.AbsolutePath.Contains("_form"))
                {
                    SPUser user = SPContext.Current.Web.CurrentUser;
                    // If anonymous
                    if (user == null)
                        response.Filter = new MOSSCleanupFilter(response.Filter, SPContext.Current.Web.Locale.Name, SPContext.Current.Web.Locale.LCID, this.CleanupConfig);
                }
            }
        }

        private XmlDocument CleanupConfig
        {
            get
            {
                if (HttpContext.Current.Cache["CleanupConfig"] == null)
                {
                    XmlDocument xmlDoc = XmlUtil.GetXmlConfigDocument(SPContext.Current.Web, ConfigurationManager.AppSettings["CleanupConfig"]);
                    if (xmlDoc != null)
                    {
                        string daysStr = ConfigurationManager.AppSettings["CleanupCacheDays"];
                        double days;
                        if (!double.TryParse(daysStr,out days))
                            days = 1;
                        HttpContext.Current.Cache.Insert("CleanupConfig", xmlDoc, null, DateTime.Now.AddDays(days), TimeSpan.Zero);
                    }
                }
                return (XmlDocument)HttpContext.Current.Cache["CleanupConfig"];
            }
        }

        public void Dispose()
        {
        }
    }
}
